
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Tablespaces &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Database access optimization" href="optimization.html" />
    <link rel="prev" title="Multiple databases" href="multi-db.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="multi-db.html" title="Multiple databases">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="optimization.html" title="Database access optimization">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-db-tablespaces">
            
  <div class="section" id="s-tablespaces">
<span id="tablespaces"></span><h1>Tablespaces<a class="headerlink" href="#tablespaces" title="Permalink to this headline">¶</a></h1>
<p>A common paradigm for optimizing performance in database systems is the use of
<a class="reference external" href="https://en.wikipedia.org/wiki/Tablespace">tablespaces</a> to organize disk layout.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Django does not create the tablespaces for you. Please refer to your
database engine’s documentation for details on creating and managing
tablespaces.</p>
</div>
<div class="section" id="s-declaring-tablespaces-for-tables">
<span id="declaring-tablespaces-for-tables"></span><h2>Declaring tablespaces for tables<a class="headerlink" href="#declaring-tablespaces-for-tables" title="Permalink to this headline">¶</a></h2>
<p>A tablespace can be specified for the table generated by a model by supplying
the <a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.db_tablespace" title="django.db.models.Options.db_tablespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">db_tablespace</span></code></a> option inside the model’s
<code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">Meta</span></code>. This option also affects tables automatically created for
<a class="reference internal" href="../../ref/models/fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManyToManyField</span></code></a>s in the model.</p>
<p>You can use the <a class="reference internal" href="../../ref/settings.html#std:setting-DEFAULT_TABLESPACE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEFAULT_TABLESPACE</span></code></a> setting to specify a default value
for <a class="reference internal" href="../../ref/models/options.html#django.db.models.Options.db_tablespace" title="django.db.models.Options.db_tablespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">db_tablespace</span></code></a>. This is useful for setting
a tablespace for the built-in Django apps and other applications whose code you
cannot control.</p>
</div>
<div class="section" id="s-declaring-tablespaces-for-indexes">
<span id="declaring-tablespaces-for-indexes"></span><h2>Declaring tablespaces for indexes<a class="headerlink" href="#declaring-tablespaces-for-indexes" title="Permalink to this headline">¶</a></h2>
<p>You can pass the <a class="reference internal" href="../../ref/models/indexes.html#django.db.models.Index.db_tablespace" title="django.db.models.Index.db_tablespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">db_tablespace</span></code></a> option to an
<code class="docutils literal notranslate"><span class="pre">Index</span></code> constructor to specify the name of a tablespace to use for the index.
For single field indexes, you can pass the
<a class="reference internal" href="../../ref/models/fields.html#django.db.models.Field.db_tablespace" title="django.db.models.Field.db_tablespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">db_tablespace</span></code></a> option to a <code class="docutils literal notranslate"><span class="pre">Field</span></code> constructor
to specify an alternate tablespace for the field’s column index. If the column
doesn’t have an index, the option is ignored.</p>
<p>You can use the <a class="reference internal" href="../../ref/settings.html#std:setting-DEFAULT_INDEX_TABLESPACE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEFAULT_INDEX_TABLESPACE</span></code></a> setting to specify
a default value for <a class="reference internal" href="../../ref/models/fields.html#django.db.models.Field.db_tablespace" title="django.db.models.Field.db_tablespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">db_tablespace</span></code></a>.</p>
<p>If <a class="reference internal" href="../../ref/models/fields.html#django.db.models.Field.db_tablespace" title="django.db.models.Field.db_tablespace"><code class="xref py py-attr docutils literal notranslate"><span class="pre">db_tablespace</span></code></a> isn’t specified and you didn’t
set <a class="reference internal" href="../../ref/settings.html#std:setting-DEFAULT_INDEX_TABLESPACE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEFAULT_INDEX_TABLESPACE</span></code></a>, the index is created in the same
tablespace as the tables.</p>
</div>
<div class="section" id="s-an-example">
<span id="an-example"></span><h2>An example<a class="headerlink" href="#an-example" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">TablespaceExample</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span> <span class="n">db_index</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="s2">&quot;indexes&quot;</span><span class="p">)</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">,</span> <span class="n">db_index</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">shortcut</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span>
    <span class="n">edges</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">to</span><span class="o">=</span><span class="s2">&quot;self&quot;</span><span class="p">,</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="s2">&quot;indexes&quot;</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">db_tablespace</span> <span class="o">=</span> <span class="s2">&quot;tables&quot;</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="p">[</span><span class="n">models</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;shortcut&#39;</span><span class="p">],</span> <span class="n">db_tablespace</span><span class="o">=</span><span class="s1">&#39;other_indexes&#39;</span><span class="p">)]</span>
</pre></div>
</div>
<p>In this example, the tables generated by the <code class="docutils literal notranslate"><span class="pre">TablespaceExample</span></code> model (i.e.
the model table and the many-to-many table) would be stored in the <code class="docutils literal notranslate"><span class="pre">tables</span></code>
tablespace. The index for the name field and the indexes on the many-to-many
table would be stored in the <code class="docutils literal notranslate"><span class="pre">indexes</span></code> tablespace. The <code class="docutils literal notranslate"><span class="pre">data</span></code> field would
also generate an index, but no tablespace for it is specified, so it would be
stored in the model tablespace <code class="docutils literal notranslate"><span class="pre">tables</span></code> by default. The index for the
<code class="docutils literal notranslate"><span class="pre">shortcut</span></code> field would be stored in the <code class="docutils literal notranslate"><span class="pre">other_indexes</span></code> tablespace.</p>
</div>
<div class="section" id="s-database-support">
<span id="database-support"></span><h2>Database support<a class="headerlink" href="#database-support" title="Permalink to this headline">¶</a></h2>
<p>PostgreSQL and Oracle support tablespaces. SQLite and MySQL don’t.</p>
<p>When you use a backend that lacks support for tablespaces, Django ignores all
tablespace-related options.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Tablespaces</a><ul>
<li><a class="reference internal" href="#declaring-tablespaces-for-tables">Declaring tablespaces for tables</a></li>
<li><a class="reference internal" href="#declaring-tablespaces-for-indexes">Declaring tablespaces for indexes</a></li>
<li><a class="reference internal" href="#an-example">An example</a></li>
<li><a class="reference internal" href="#database-support">Database support</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="multi-db.html"
                        title="previous chapter">Multiple databases</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="optimization.html"
                        title="next chapter">Database access optimization</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/topics/db/tablespaces.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="multi-db.html" title="Multiple databases">previous</a>
     |
    <a href="../index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="optimization.html" title="Database access optimization">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>